package com.morn.fanfou.fragment;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.text.Html;
import android.text.TextUtils;
import android.text.method.LinkMovementMethod;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.ImageView;
import android.widget.ProgressBar;
import android.widget.TextView;
import android.widget.Toast;

import com.morn.fanfou.R;
import com.morn.fanfou.UserViewActivity;
import com.morn.fanfou.model.Status;
import com.morn.fanfou.util.DateUtil;

public class StatusViewFragment extends Fragment implements OnClickListener {

	private Status status;
	private ImageView photoView;
	private TextView nameView;
	private TextView textView;
	private View replayStatusLayout;
	private TextView replyTextView;
	private TextView timeView;
	private TextView sourceView;
	private Button resendButton;
	private Button replyButton;
	private String repalyStatusId;
	private CheckBox checkBox;
	private ProgressBar progressBar;
	private View userView;

	@Override
	public void onAttach(Activity activity) {
		super.onAttach(activity);
		status = (Status) getArguments().getSerializable("status");
		repalyStatusId = status.getIn_reply_to_status_id();

	}

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		View v = inflater.inflate(R.layout.status_view, container, false);
		TextView titleText = (TextView) v.findViewById(R.id.header_title_text);
		titleText.setText(R.string.header_title_status);

		photoView = (ImageView) v.findViewById(R.id.status_view_photo);

		nameView = (TextView) v.findViewById(R.id.status_view_name);
		nameView.setText(status.getUser().getName());

		resendButton = (Button) v.findViewById(R.id.status_view_resend_button);
		resendButton.setOnClickListener(this);

		replyButton = (Button) v.findViewById(R.id.status_view_reply_button);
		replyButton.setOnClickListener(this);

		textView = (TextView) v.findViewById(R.id.status_view_text);
		textView.setText(Html.fromHtml(status.getText()));
		textView.setMovementMethod(LinkMovementMethod.getInstance());

		timeView = (TextView) v.findViewById(R.id.status_view_time);
		timeView.setText(DateUtil.formatDate(status.getCreated_at(),
				"yy-MM-dd HH:mm:ss"));

		sourceView = (TextView) v.findViewById(R.id.status_view_source);
		sourceView.setText(Html.fromHtml(status.getSource()));
		sourceView.setMovementMethod(LinkMovementMethod.getInstance());

		checkBox = (CheckBox) v.findViewById(R.id.status_view_checkbox);
		checkBox.setOnClickListener(this);

		progressBar = (ProgressBar) v
				.findViewById(R.id.status_view_checkbox_progressbar);
		progressBar.setVisibility(View.GONE);

		userView = v.findViewById(R.id.status_view_user);
		userView.setOnClickListener(this);

		replayStatusLayout = v.findViewById(R.id.status_view_reply_status);
		if (TextUtils.isEmpty(repalyStatusId)) {
			replayStatusLayout.setVisibility(View.GONE);
		} else {
			replyTextView = (TextView) v
					.findViewById(R.id.status_view_reply_text);
			replyTextView.setText(repalyStatusId);
		}

		return v;
	}

	@Override
	public void onActivityCreated(Bundle savedInstanceState) {
		super.onActivityCreated(savedInstanceState);
	}

	@Override
	public void onClick(View v) {
		int viewId = v.getId();
		if (viewId == R.id.status_view_resend_button) {
			resendStatus();
		} else if (viewId == R.id.status_view_reply_button) {
			replyStatus();
		} else if (viewId == R.id.status_view_user) {
			showUser();
		} else if (viewId == R.id.status_view_checkbox) {
			favoriteClick();
		}
	}

	private void showUser() {
		Intent intent = new Intent(this.getActivity(), UserViewActivity.class);
		intent.putExtra("user", status.getUser());
		this.getActivity().startActivity(intent);

	}

	private void resendStatus() {
		Toast.makeText(this.getActivity(), "resendStatus", Toast.LENGTH_SHORT)
				.show();

	}

	private void replyStatus() {
		Toast.makeText(this.getActivity(), "replyStatus", Toast.LENGTH_SHORT)
				.show();

	}

	private void favoriteClick() {
		Toast.makeText(this.getActivity(), "favoriteClick", Toast.LENGTH_SHORT)
				.show();
	}

}
